import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:znmm/UI/A_Common/custom_web_view.dart';
import 'package:znmm/UI/A_Common/image/cached_network_Image.dart';

import '../model/news_model.dart';

class NewsCell extends StatelessWidget {
  final NewsModel pmodel;

  const NewsCell({super.key, required this.pmodel});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        Get.to(CustomWebView(
          titleStr: pmodel.sourceName,
          initialUrl: pmodel.sourceUrl,
        ));
      },
      child: Container(
        alignment: Alignment.topLeft,
        //color: Colors.grey,
        margin: const EdgeInsets.fromLTRB(0, 0, 0, 10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start, // 从左侧开始布局
          crossAxisAlignment: CrossAxisAlignment.start, // 从顶部开始布局
          children: [
            //------左边黑方块
            Container(
              margin: const EdgeInsets.fromLTRB(27.5, 5, 25, 0),
              alignment: Alignment.topLeft,
              width: 5,
              height: 10,
              decoration: BoxDecoration(
                color: pmodel.imptType == 0 ? Colors.black : Colors.red,
                borderRadius: BorderRadius.circular(1),
              ),
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                  width: MediaQuery.of(context).size.width - 100,
                  child: Text(
                    pmodel.occurrenceTime,
                    textAlign: TextAlign.left,
                    //overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      color: pmodel.imptType == 0 ? Colors.black : Colors.red,
                    ),
                  ),
                ),
                const SizedBox(
                  height: 8,
                ),
                if (pmodel.iType == 1)
                  _imageType(context)
                else if (pmodel.iType == 2)
                  _textRichText(context)
                else
                  _normalType(context),
              ],
            ),
          ],
        ),
      ),
    );
  }

  //图文一起
  Widget _textRichText(BuildContext context) {
    return SizedBox(
        width: MediaQuery.of(context).size.width - 80,
        child: RichText(
          text: TextSpan(
            style: const TextStyle(
              color: Colors.black,
              fontSize: 18,
            ),
            children: [
              WidgetSpan(
                child: SizedBox(
                  width: 20,
                  height: 20,
                  child: CachedNetworkImageLoader(
                    imageUrl: pmodel.sourceIcon,
                  ),
                ),
              ),
              const WidgetSpan(
                child: SizedBox(width: 8), // 添加间距
              ),
              TextSpan(
                text: pmodel.text,
              ),
            ],
          ),
        ));
  }

  //------正常
  Widget _normalType(BuildContext context) {
    return SizedBox(
      //color: Colors.amber,
      width: MediaQuery.of(context).size.width - 90,
      child: Text(
        pmodel.text,
        textAlign: TextAlign.left,
        style: TextStyle(
          fontSize: 16,
          color: pmodel.imptType == 0 ? Colors.black : Colors.red,
        ),
      ),
    );
  }

  //------图片
  Widget _imageType(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width - 90,
      margin: const EdgeInsets.only(top: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start, // 从左侧开始布局
        crossAxisAlignment: CrossAxisAlignment.start, // 从顶部开始布局
        children: [
          pmodel.sourceIcon.isNotEmpty
              ? SizedBox(
                  width: 150,
                  height: 150,
                  child: CachedNetworkImageLoader(
                    imageUrl: pmodel.sourceIcon,
                  ),
                )
              : ClipRRect(
                  borderRadius: BorderRadius.circular(8.0),
                  child: Image.asset(pmodel.icon, width: 150, height: 150),
                ),
          const SizedBox(
            width: 5,
          ),
          Expanded(
            child: Text(
              pmodel.text,
              textAlign: TextAlign.left,
              style: TextStyle(
                color: pmodel.imptType == 0 ? Colors.black : Colors.red,
              ),
            ),
          )
        ],
      ),
    );
  }
}
